import 'package:tracker/theme/app_theme.dart';
import 'package:tracker/utils/app_utils.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:tracker/theme/app_theme.dart';

import '../../../startup/config/app_start.dart';

/// app打开时的第一个页面
class GodPage extends StatefulWidget {
  const GodPage({super.key});

  @override
  State<GodPage> createState() => _GodPageState();
}
Future<void> _initAppConfig() async {
  print('_initAppConfig');

  try {
    await AppStart.initConfig();      // 初始化完成后，可以触发状态更新（例如通知 Provider）
    // 初始化极光推送
    // await JPushUtils.initJPush();
    // 例如：通过 Provider 监听配置变化
  } catch (e) {
    // 处理错误
    print('Appstart 初始化失败: $e');
  }
}
class _GodPageState extends State<GodPage> {
  @override
  Widget build(BuildContext context) {
    const fontSize = 16.0;
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: const BoxDecoration(
        color: Colors.white,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Column(
            children: [
              const SizedBox(
                height: 40,
              ),
              const Image(
                image: AssetImage('assets/images/logo/xjww_logo.png'),
                width: 80,
              ),
              const SizedBox(
                height: 12,
              ),
              Text(
                '星迹途联',
                style: TextStyle(
                  fontSize: 20,
                  color: AppTheme.themeConfig.primaryTextColor,
                ),
              ),
              const SizedBox(
                height: 12,
              ),
              Text(
                '欢迎使用星迹途联。你可以使用本应用实现智能设备的添加、控制、共享和场景联动等功能。',
                style: TextStyle(
                  fontSize: 17,
                  color: AppTheme.themeConfig.primaryTextColor,
                ),
                textAlign: TextAlign.center,
              )
            ],
          ),
          Column(
            children: [
              Text(
                '本应用需要联网来提供更多服务，如您阅读并同意用户服务协议与隐私政策且同意联网，请点击下方同意按钮进入服务。',
                style: TextStyle(
                  fontSize: 13,
                  color: AppTheme.themeConfig.infoTextColor,
                ),
                textAlign: TextAlign.center,
              ),
              const SizedBox(
                height: 12,
              ),
              Text.rich(
                TextSpan(
                  text: '如您阅读并同意',
                  style: TextStyle(
                    fontSize: fontSize,
                    color: AppTheme.themeConfig.primaryTextColor,
                  ),
                  children: [
                    TextSpan(
                      text: '《用户服务协议》',
                      style: TextStyle(
                        fontSize: fontSize,
                        color: AppTheme.themeConfig.primaryColor,
                      ),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          context.pushNamed('protocol', queryParameters: {
                            'title': '用户协议',
                            'url': 'https://xjtl.tech/user_agreement/index.html'
                          });
                        },
                    ),
                    TextSpan(
                      text: '和',
                      style: TextStyle(
                        fontSize: fontSize,
                        color: AppTheme.themeConfig.primaryTextColor,
                      ),
                    ),
                    TextSpan(
                      text: '《隐私政策》',
                      style: TextStyle(
                        fontSize: fontSize,
                        color: AppTheme.themeConfig.primaryColor,
                      ),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          context.pushNamed('protocol', queryParameters: {
                            'title': '隐私政策',
                            'url': 'https://xjtl.tech/privacy_policy/index.html'
                          });
                        },
                    ),
                    TextSpan(
                      text: '，请点击下方同意按钮进入服务',
                      style: TextStyle(
                        fontSize: fontSize,
                        color: AppTheme.themeConfig.primaryTextColor,
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(
                height: 20,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  TDButton(
                    text: '退出',
                    size: TDButtonSize.large,
                    type: TDButtonType.fill,
                    shape: TDButtonShape.round,
                    width: 150,
                    style: TDButtonStyle(
                      backgroundColor: Colors.grey[200],
                    ),
                    onTap: () {
                      // 退出app
                      AppUtils.exitApp(context);
                    },
                  ),
                  TDButton(
                    text: '同意并继续',
                    size: TDButtonSize.large,
                    type: TDButtonType.fill,
                    shape: TDButtonShape.round,
                    width: 150,
                    style: TDButtonStyle(
                      backgroundColor: AppTheme.themeConfig.primaryColor,
                    ),
                    textStyle: const TextStyle(
                      color: Colors.white,
                    ),
                    onTap: () async {
                      // 同意协议
                      await SpUtil.putBool('hasAgreed', true);
                      await _initAppConfig();

                      // 进入app
                      context.replace('/');
                    },
                  )
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}
